﻿
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Security;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
namespace WCYCReader.ImageTool
{
    public static class ImageExtensions
    {
        public static WriteableBitmap ToBitmap(this ImageBase image)
        {
            Contract.Requires<ArgumentNullException>(image != null, "Image cannot be null.");
            Contract.Requires<ArgumentException>(image.IsLoaded, "Image has not been loaded.");
            return image.ToBitmap(null);
        }

        public static WriteableBitmap ToBitmap(this ImageBase image, IImageFilter filter)
        {
            Contract.Requires<ArgumentNullException>(image != null, "Image cannot be null.");
            Contract.Requires<ArgumentException>(image.IsLoaded, "Image has not been loaded.");
            WriteableBitmap bitmap = new WriteableBitmap(image.PixelWidth, image.PixelHeight);
            ImageBase base2 = image;
            if (filter != null)
            {
                base2 = new ImageBase(image);
                filter.Apply(base2);
            }
            byte[] pixels = base2.Pixels;
            if (pixels != null)
            {
                int[] dst = bitmap.Pixels;
                if (dst != null)
                {
                    Buffer.BlockCopy(pixels, 0, dst, 0, pixels.Length);
                    for (int i = 0; i < dst.Length; i++)
                    {
                        int num2 = dst[i];
                        int num3 = (num2 >> 0x18) & 0xff;
                        float num4 = ((float) num3) / 255f;
                        dst[i] = (((num3 << 0x18) | (((int) ((num2 & 0xff) * num4)) << 0x10)) | (((int) (((num2 >> 8) & 0xff) * num4)) << 8)) | ((int) (((num2 >> 0x10) & 0xff) * num4));
                    }
                }
            }
            bitmap.Invalidate();
            return bitmap;
        }

        public static Image ToImage(this WriteableBitmap bitmap)
        {
            Contract.Requires<ArgumentNullException>(bitmap != null, "Bitmap cannot be null.");
            bitmap.Invalidate();
            Contract.Assume(bitmap.PixelWidth >= 0);
            Contract.Assume(bitmap.PixelHeight >= 0);
            Image image = new Image(bitmap.PixelWidth, bitmap.PixelHeight);
            try
            {
                byte[] pixels = image.Pixels;
                int[] numArray = bitmap.Pixels;
                if (numArray == null)
                {
                    return image;
                }
                for (int i = 0; i < image.PixelHeight; i++)
                {
                    for (int j = 0; j < image.PixelWidth; j++)
                    {
                        int index = (bitmap.PixelWidth * i) + j;
                        int num4 = numArray[index];
                        byte num5 = (byte) ((num4 >> 0x18) & 0xff);
                        float num6 = ((float) num5) / 255f;
                        if (num6 > 0f)
                        {
                            byte num7 = (byte) (((float) ((num4 >> 0x10) & 0xff)) / num6);
                            byte num8 = (byte) (((float) ((num4 >> 8) & 0xff)) / num6);
                            byte num9 = (byte) (((float) (num4 & 0xff)) / num6);
                            pixels[index * 4] = num7;
                            pixels[(index * 4) + 1] = num8;
                            pixels[(index * 4) + 2] = num9;
                            pixels[(index * 4) + 3] = num5;
                        }
                    }
                }
            }
            catch (SecurityException exception)
            {
                throw new ArgumentException("Bitmap cannot accessed", exception);
            }
            return image;
        }

        public static Image ToImage(this UIElement element)
        {
            Contract.Requires<ArgumentNullException>(element != null, "UI element cannot be null.");
            WriteableBitmap bitmap = new WriteableBitmap(element, new TranslateTransform());
            return bitmap.ToImage();
        }

        public static void WriteToStream(this Image image, Stream stream, string fileName)
        {
            Contract.Requires<ArgumentNullException>(image != null, "Image cannot be null.");
            Contract.Requires<ArgumentException>(image.IsLoaded, "Image has not been loaded.");
            Contract.Requires<ArgumentNullException>(stream != null, "Stream cannot be null.");
            Contract.Requires<ArgumentException>(!string.IsNullOrEmpty(fileName), "File name cannot be null or empty.");
            string str = null;
            if (fileName != null)
            {
                int startIndex = fileName.LastIndexOf(".");
                if (startIndex >= 0)
                {
                    str = fileName.Substring(startIndex);
                }
            }
            if ((str == null) || string.IsNullOrEmpty(str))
            {
                throw new ArgumentException("The file name is not valid and contains no extension.");
            }
            string str2 = str.Substring(1);
            Contract.Assume(!string.IsNullOrEmpty(str2));
            IImageEncoder encoder = null;
            foreach (IImageEncoder encoder2 in Encoders.GetAvailableEncoders())
            {
                if ((encoder2 != null) && encoder2.IsSupportedFileExtension(str2))
                {
                    encoder = encoder2;
                    break;
                }
            }
            if (encoder == null)
            {
                throw new UnsupportedImageFormatException("Specified file extension is not supported.");
            }
            encoder.Encode(image, stream);
            stream.Close();
        }
    }
}

